<?php
// $Id$

/**
 * Controller_Links 控制器
 */
class Controller_Links extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		$page = (int)$this->_context->page;
		if ($page==0) $page++;
		//echo $page;exit;
		$limit = $this->_context->limit ? $this->_context->limit : 15;
		$q = Links::find()->order('taxis desc,id desc')->limitPage($page, $limit);
		$list = $q->getAll()->toArray();
		$this->_view['pager'] = $q->getPagination();
		$this->_view['list'] = $list;
		$this->_view['start'] = ($page-1)*$limit;
		$this->_view['subject'] = "友情链接管理";
	}
	function actionCreate(){
		$rootdir = dirname(dirname(dirname(dirname(__FILE__)))).DIRECTORY_SEPARATOR;
		$error_pic_msg = '';
		$alert = '';
		$this->_view['subject'] = "友情链接管理";
		$this->_view['error_pic_msg'] = $error_pic_msg;		
		if ($this->_context->isPOST() && isset($_POST['name'])){
			@extract($_POST);						
			$name 	= addslashes(trim($name));
			$url = addslashes(trim($url));
			$taxis = intval($taxis);
			$form_value = array(
							'name'		=> $name,
							'url'		=> $url,
							'taxis'		=> $taxis,
							);
			$pic = '';
			$file_pic = $_FILES['pic'];
			if($file_pic['size']){
				$temp=$file_pic['tmp_name'];
				$filename = $file_pic["name"];	
				$filetype=substr(strrchr($filename,"."),1);	
				$filesize = $file_pic["size"];
				$filetype = strtolower($filetype);
				$refusetype = array("jpg","png","gif");
				if((!in_array($filetype,$refusetype) || ($filesize > 1024*1024))){
					if(!in_array($filetype,$refusetype)){
						$error_pic_msg = "请上传图片文件！";
					}
					if($filesize > 1024*1024){
						$error_pic_msg .= "文件超过1M，不允许上传！";
					}
					$this->_view['error_pic_msg'] = $error_pic_msg;
					return;
				}	
				$source = $temp;
				$desfilename = time().'.'.$filetype;
				$my_dir = 'upload_files'.DIRECTORY_SEPARATOR.'links'.DIRECTORY_SEPARATOR;
				$date_dir = Helper_Util::mk_date_dir($rootdir.$my_dir);
				$pic = $my_dir.$date_dir.$desfilename;
				$destination = $rootdir.$pic;
				//echo $destination.'<br />';
				copy($source,$destination);
				$form_value['pic'] = $pic;
			}
			//print_r($form_value);
			$links = new Links($form_value);
			$links->save();
			$alert = "<script language='javascript'>if(confirm('友情链接添加成功，是否继续添加？')){window.open('".url('links/create')."','_self');}else{window.open('".url('links')."','_self');}</script>";
			
		}
		$this->_view['alert'] = $alert;		
	}
	function actionEdit(){
		$rootdir = dirname(dirname(dirname(dirname(__FILE__)))).DIRECTORY_SEPARATOR;
		$error_pic_msg = '';
		$alert = '';
		$this->_view['rootdir'] = $rootdir;
		$this->_view['subject'] = "友情链接管理";
		
		$id = $this->_context->id;
		$links = Links::find()->getById($id);	
		if ($this->_context->isPOST() && isset($_POST['name'])){
			@extract($_POST);						
			$name 	= addslashes(trim($name));
			$url = addslashes(trim($url));
			$taxis = intval($taxis);
			$form_value = array(
							'id'			=> $id,
							'name'		=> $name,
							'url'		=> $url,
							'taxis'		=> $taxis,
							);
			$pic = '';
			if(isset($_FILES['pic'])){
			$file_pic = $_FILES['pic'];
			if($file_pic['size']){
				$temp=$file_pic['tmp_name'];
				$filename = $file_pic["name"];	
				$filetype=substr(strrchr($filename,"."),1);	
				$filesize = $file_pic["size"];
				$filetype = strtolower($filetype);
				$refusetype = array("jpg","png","gif");
				if((!in_array($filetype,$refusetype) || ($filesize > 1024*1024))){
					if(!in_array($filetype,$refusetype)){
						$error_pic_msg = "请上传图片文件！";
					}
					if($filesize > 1024*1024){
						$error_pic_msg .= "文件超过1M，不允许上传！";
					}
					$this->_view['error_pic_msg'] = $error_pic_msg;
					return;
				}	
				$source = $temp;
				$desfilename = time().'.'.$filetype;
				$my_dir = 'upload_files'.DIRECTORY_SEPARATOR.'links'.DIRECTORY_SEPARATOR;
				$date_dir = Helper_Util::mk_date_dir($rootdir.$my_dir);
				$pic = $my_dir.$date_dir.$desfilename;
				$destination = $rootdir.$pic;
				//echo $destination.'<br />';
				copy($source,$destination);
				$form_value['pic'] = $pic;
			}
			}
			//print_r($form_value);
			//$log_rec = Helper_Util::toArray($links);
			$links -> changeProps($form_value);
			$links -> save();
			//Log::addlog(1, 'Links', $links->id(), $log_rec, '修改友情链接：'.$links->name, NULL, 'links');
			$alert = "<script language='javascript'>if(confirm('该条友情链接修改成功，是否继续修改？')){window.open('".url('links/edit',array('id'=>$id))."','_self');}else{window.open('".url('links')."','_self');}</script>";
		}
	 	
	 	$myData = $links->toArray();
	 	if(isset($_GET['actionValue'])){
	 		$actionValue = $_GET['actionValue'];
	 		if($actionValue == 'deletePic'){
	 			if(is_file($rootdir.$myData['pic'])){
	 				unlink($rootdir.$myData['pic']);
	 				$myData['pic'] = '';
	 				$form_value_pic = array(
							'id'			=> $id,
							'pic'			=> '',
							);
					$links -> changeProps($form_value_pic);
					$links -> save();
	 				$alert = "<script language='javascript'>alert('图片已经删除！');</script>";
	 			}
	 		}
	 	}
	 	$this->_view['myData'] = $myData;
		
		$this->_view['error_pic_msg'] = $error_pic_msg;	
		$this->_view['alert'] = $alert;
	}
	//删除
	function actionDelete(){
		$rootdir = dirname(dirname(dirname(dirname(__FILE__)))).DIRECTORY_SEPARATOR;
		$id = $this->_context->id;
		$links = Links::find()->getById($id);
		//$log_rec = Helper_Util::toArray($links);
		$myData = $links->toArray();
		if(is_file($rootdir.$myData['pic'])){
 			unlink($rootdir.$myData['pic']);
 		}
		Links::meta()->destroyWhere('id = ?', $id);
		//Log::addlog(2, 'Links', $links->id(), $log_rec, '删除友情链接：'.$links->name, NULL, 'links');
		return $this->_redirect(url('links'));
	}
}


